<?php
	if (isset($_POST['editer_cat'])) 
	{
		$req = $bdd->prepare('UPDATE faq_categories SET nom = :nom, description = :description WHERE id=:id');
		$req->execute(array(
			'nom' => $_POST['nom'],
			'description' => $_POST['description'],
			'id' => $_POST['id']
		));
		header('Location: page.php?page=admin_faq'); 
	}
	elseif (isset($_POST['effacer_cat'])) 
	{
		$req = $bdd->prepare('DELETE FROM faq_categories WHERE id=:id');
		$req->execute(array('id' => $_POST['id']));
		$req = $bdd->prepare('DELETE FROM faq_questions WHERE id_categories=:id');
		$req->execute(array('id' => $_POST['id']));
		header('Location: page.php?page=admin_faq'); 
	}
	elseif (isset($_POST['ajouter_cat'])) 
	{
		$req = $bdd->prepare('INSERT INTO faq_categories (nom, description) VALUES (:nom, :description)');
		$req->execute(array(
			'nom' => $_POST['nom'],
			'description' => $_POST['description']
		));
		header('Location: page.php?page=admin_faq');
	}
	elseif (isset($_POST['editer_que'])) 
	{
		$req = $bdd->prepare('UPDATE faq_questions SET questions = :questions, reponses = :reponses WHERE id=:id');
		$req->execute(array(
			'questions' => $_POST['questions'],
			'reponses' => $_POST['reponses'],
			'id' => $_POST['id']
		));
		header('Location: page.php?page=admin_faq'); 
	}
	elseif (isset($_POST['effacer_que'])) 
	{
		$req = $bdd->prepare('DELETE FROM faq_questions WHERE id_categories=:id');
		$req->execute(array('id' => $_POST['id']));
		header('Location: page.php?page=admin_faq'); 
	}
	elseif (isset($_POST['ajouter_que'])) 
	{
		$req = $bdd->prepare('INSERT INTO faq_questions (id_categories, questions, reponses) VALUES (:id_categories, :questions, :reponses)');
		$req->execute(array(
			'id_categories' => $_POST['id_categorie'],
			'questions' => $_POST['questions'],
			'reponses' => $_POST['reponses']
		));
		header('Location: page.php?page=admin_faq');
	}

	if ((isset($_SESSION['identifiant'])) AND (isset($_SESSION['groupe'])==2))
	{
		$req = $bdd->query('SELECT * FROM faq_categories ORDER BY id');
		?>
		<h1 id="titre">Gestion de la FAQ : Catégories</h1>
		<table width="100%">
			<?php
			while ($donnees = $req->fetch())
			{
				?>
				<form method="post" action="page.php?page=admin_faq">
					<tr><td><input type="text" name="nom" id="nom" value="<?php echo $donnees['nom']; ?>" maxlength="250" size="40"></td>
					<td><input type="submit" value="<?php echo EDITER; ?>" name="editer_cat" />
					    <input type="submit" value="<?php echo EFFACER; ?>" name="effacer_cat" /></td></tr>
					<tr><td colspan="2"><input type="text" name="description" id="description" value="<?php echo $donnees['description']; ?>" maxlength="250" size="80">
					    <input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" /></td></tr>
				</form>
				<?php 
			}
			?>
			<tr><td colspan="3"><hr /></td></tr>
			<?php
			$req->closeCursor();
			?>
			<form method="post" action="page.php?page=admin_faq">
				<tr><td><input type="text" name="nom" id="nom" value="<?php echo $donnees['nom']; ?>" maxlength="250" size="40"></td>
				<td><input type="submit" value="Ajouter cette catégorie" name="ajouter_cat" /></td></tr>
				<tr><td colspan="2"><input type="text" name="description" id="description" value="<?php echo $donnees['description']; ?>" maxlength="250" size="80"><input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" /></td></tr>
			</form>
		</table>
		<?php
		$req = $bdd->query('SELECT * FROM faq_questions ORDER BY id_categories,id');
		?>
		<h1>Gestion de la FAQ : Questions/réponses</h1>
		<table border="0">
			<?php
			while ($donnees = $req->fetch())
			{
				$req_cat = $bdd->prepare('SELECT * FROM faq_categories');
				$req_cat->execute(array('id_categories' => $donnees['id_categories']));
				?>
				<form method="post" action="page.php?page=admin_faq">
					<tr><td colspan="2">
					<select name="id_categorie">
						<?php
						while ($donnees_cat = $req_cat->fetch())
						{
							if($donnees_cat['id'] == $donnees['id_categories']) { ?><option value="<?php echo $donnees_cat['id']; ?>" selected="selected"><?php echo $donnees_cat['nom']; ?></option><?php }
							else { ?><option value="<?php echo $donnees_cat['id']; ?>"><?php echo $donnees_cat['nom']; ?></option><?php }
							
						}
						?>
					</select>
					<input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" /></td></tr>
					<tr><td><input type="text" name="questions" id="questions" value="<?php echo $donnees['questions']; ?>" maxlength="250" size="60"></td>
						<td><input type="submit" value="<?php echo EDITER; ?>" name="editer_que" />
					    <input type="submit" value="<?php echo EFFACER; ?>" name="effacer_que" /></td></tr>
					<tr><td colspan="2"><input type="text" name="reponses" id="reponses" value="<?php echo $donnees['reponses']; ?>" maxlength="250" size="90"></td></tr>
				</form>
				<?php 
			}
			?>
			<tr><td colspan="3"><hr /></td></tr>
			<?php
			$req->closeCursor();
			$i=$donnees['id_categories'];
			?>
			<form method="post" action="page.php?page=admin_faq">
				<tr><td>
				<select name="id_categorie">
					<?php
					$req_cat = $bdd->prepare('SELECT * FROM faq_categories');
					$req_cat->execute(array('id_categories' => $donnees['id_categories']));
					while ($donnees_cat = $req_cat->fetch())
					{
						?><option value="<?php echo $donnees_cat['id']; ?>"><?php echo $donnees_cat['nom']; ?></option><?php
					}
					?>
				</select>
				</td><td><input type="submit" value="Ajouter cette question" name="ajouter_que" /></td></tr>
				<tr><td colspan="2"><input type="text" name="questions" id="questions" value="" maxlength="250" size="90"></td></tr>
				<tr><td colspan="2"><input type="text" name="reponses" id="reponses" value="" maxlength="250" size="90"><input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" /></td></tr>
			</form>
		</table>
		<?php
	}
	else
	{
		echo NON_ACCES_ADMIN;
	}
?>